Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
Add the ability to render comments when the comment-list elements should be filled with text "Loading..." and show results one by one

I'm trying to render results one by one with some text like "Loading..." and show results one by one. I found answers at one, two and tried to solve this but it shows only one loading text and renders, shows all results at a time. The results should appear one after the other, with the order preserved. If any error occurs during the loading, the tag should be left empty. How to solve this challenge? For full question please refer here

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Render Comments</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="comment-list" data-count=1></div>
        <script type="text/javascript" src="./app.js"></script>
    </body>
</html>

JS

"use-strict";

var commentList = $(".comment-list");
var count = $(".comment-list").data("count");
var url = "https://jsonplaceholder.typicode.com/comments?postId=" + count;

$.ajax({
  url: url,
  method: "GET",
  beforeSend: function () {
    commentList.html("Loading...");
  },
  success: function (data) {
    commentList.empty();
    data.forEach(function (comment) {
      commentList.append(`
           <div class="comment-item">
              <div class="comment-item__username">${comment.id}</div>
              <div class="comment-item__message">${comment.name}</div>
          </div>`);
    });
  },
  error: function () {
    // commentList.empty();
    data.forEach(function () {
    commentList.append(`
         <div class="comment-item">
         <div>Oops! Error occured to show the results</div>
         </div>`);
    });
  },
});

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You might consider doing this with CSS instead of with javascript. There are many examples including link. If you insist on doing this with javascript you need to break up the code adding the elements to the page using setInterval or setTimeout.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda